import axios from "axios";

const http=axios.create({
    baseURL: 'http://localhost:8888/',
    timeout: 1000,
    headers: {
        'content-type': 'application/json'
    }
});

//请求前拦截
http.interceptors.request.use(req=>{
    let token=sessionStorage.getItem("token");
    if(token){
        req.headers['token']=token;
    }
    return req
},err=>{
    return Promise.reject(err)
});

//响应拦截
http.interceptors.response.use(res=>{
    return res
},err=>{
    if(err.res.status==401){
        //重新登录
        window.location.href="/"
        //移除sessionStorage的token
        sessionStorage.removeItem("token");
    }
    return Promise.reject(err)
})

export default http;
